package com.zeekr.orm.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeekr.orm.common.CommonConstant;
import com.zeekr.orm.common.ErrorCode;
import com.zeekr.orm.exception.BusinessException;
import com.zeekr.orm.exception.ThrowUtils;
import com.zeekr.orm.mapper.UserMapper;
import com.zeekr.orm.model.dto.user.UserQueryRequest;
import com.zeekr.orm.model.entity.User;
import com.zeekr.orm.model.vo.LoginUserVO;
import com.zeekr.orm.model.vo.UserVO;
import com.zeekr.orm.service.UserService;
import com.zeekr.orm.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.zeekr.orm.constant.UserConstant.USER_LOGIN_STATE;

/**
 * @author 123
 * @description 针对表【user(用户)】的数据库操作Service实现
 * @createDate 2024-11-09 04:53:29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

  public static final String SALT = "yupi";

  @Override
  public QueryWrapper<User> getQueryWrapper(UserQueryRequest userQueryRequest) {
    if (null == userQueryRequest) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
    }
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    Long id = userQueryRequest.getId();
    String unionid = userQueryRequest.getUnionid();
    String mpopenid = userQueryRequest.getMpopenid();
    String username = userQueryRequest.getUsername();
    String userprofile = userQueryRequest.getUserprofile();
    String userrole = userQueryRequest.getUserrole();
    String sortField = userQueryRequest.getSortField();
    String sortOrder = userQueryRequest.getSortOrder();
    queryWrapper.eq(id != null, "id", id);
    queryWrapper.eq(unionid != null, "unionid", unionid);
    queryWrapper.eq(mpopenid != null, "mpopenid", mpopenid);
    queryWrapper.eq(StringUtils.isNotBlank(userrole), "userrole", userrole);
    queryWrapper.like(StringUtils.isNotBlank(username), "username", username);
    queryWrapper.like(StringUtils.isNotBlank(userprofile), "userprofile", userprofile);
    queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
    return queryWrapper;
  }

  @Override
  public UserVO getUserVO(User user) {
    if (null == user) {
      return null;
    }
    UserVO userVO = new UserVO();
    BeanUtils.copyProperties(user, userVO);
    return userVO;
  }

  @Override
  public List<UserVO> getUserVO(List<User> userList) {
    if (CollUtil.isEmpty(userList)) {
      return new ArrayList<>();
    }
    return userList.stream().map(this::getUserVO).collect(Collectors.toList());
  }

  @Override
  public User getLoginUser(HttpServletRequest request) {
    Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
    User currentUser = (User) userObj;
    if (null == currentUser || null == currentUser.getId()) {
      throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
    }
    Long id = currentUser.getId();
    currentUser = this.getById(id);
    if (null == currentUser) {
      throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
    }
    return currentUser;
  }

  @Override
  public LoginUserVO userLogin(String userAccount, String userPassword, HttpServletRequest request) {
    ThrowUtils.throwIf(StringUtils.isAnyBlank(userAccount, userPassword), ErrorCode.PARAMS_ERROR);
    ThrowUtils.throwIf(userAccount.length() < 4 || userPassword.length() < 8, ErrorCode.PARAMS_ERROR, "密码错误");
    String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
    User user = this.baseMapper.selectOne(new QueryWrapper<User>().eq("userAccount", userAccount).eq("userPassword", encryptPassword));
    if (null == user) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户名或密码错误");
    }
    request.getSession().setAttribute(USER_LOGIN_STATE, user);
    return this.getLoginUserVO(user);
  }

  @Override
  public LoginUserVO getLoginUserVO(User user) {
    if (null == user) {
      return null;
    }
    LoginUserVO loginUserVO = new LoginUserVO();
    BeanUtils.copyProperties(user, loginUserVO);
    return loginUserVO;
  }
}



